package com.test.shiro;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "user/login";
    }

    @RequestMapping("/unAuth")
    public String unAuth() {
        return "user/unAuth";
    }

    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        //使用shiro编写认证操作
        //获取Subject
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //执行登录方法
        try {
            //只要执行login方法，就会去执行UserRealm中的认证逻辑
            subject.login(token);

            //如果没有异常，代表登录成功
            //跳转到textThymeleaf页面，代表主页
            return "redirect:/user/index";
        } catch (UnknownAccountException e) {
            //登录失败
            model.addAttribute("msg", "用户名不存在");
            return "user/login";

        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码错误");
            return "user/login";
        }
    }
    /**
     * 方式一：返回ModelAndView
     */
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView();
        // 设置跳转的视图 默认映射到 src/main/resources/templates/{viewName}.html
        view.setViewName("user/index");
        // 设置属性
        view.addObject("title", "我的templates页面");
        view.addObject("desc", "欢迎进入我的csdn博客");
        Author author = new Author();
        author.setAge(18);
        author.setEmail("xx@163.com");
        author.setName("xx");
        view.addObject("author", author);
        return view;
    }

    /**
     * 方式二：返回String
     * 注意：此方式不可以使用@RestController，@RestController 等价
     *      于 @Controller 加上 @ResponseBody，@ResponseBody表示
     *      该方法的返回不会被解析为跳转, 而是直接写入http响应正文。
     */
    @RequestMapping("/index1")
    public String index1(HttpServletRequest request) {
        // TODO 与上面的写法不同，但是结果一致。
        // 设置属性
        request.setAttribute("title", "我的templates页面");
        request.setAttribute("desc", "欢迎进入我的csdn博客");
        Author author = new Author();
        author.setAge(18);
        author.setEmail("xx@163.com");
        author.setName("xx");
        request.setAttribute("author", author);
        // 返回的 index 则会映射到 src/main/resources/templates/index.html
        return "user/index";
    }

    class Author {
        private int age;
        private String name;
        private String email;

        /**
         * 获取 age.
         *
         * @return age
         */
        public int getAge() {
            return age;
        }

        /**
         * 设置 age.
         *
         * @param age age
         */
        public void setAge(final int age) {
            this.age = age;
        }

        /**
         * 获取 name.
         *
         * @return name
         */
        public String getName() {
            return name;
        }

        /**
         * 设置 name.
         *
         * @param name name
         */
        public void setName(final String name) {
            this.name = name;
        }

        /**
         * 获取 email.
         *
         * @return email
         */
        public String getEmail() {
            return email;
        }

        /**
         * 设置 email.
         *
         * @param email email
         */
        public void setEmail(final String email) {
            this.email = email;
        }
    }

    @RequestMapping("/add")
    public String add() {
        return "user/add";
    }

    @RequestMapping("/update")
    public String update() {
        return "user/update";
    }
}
